package ru.gerunn.parsing.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.zip.ZipInputStream;

import ru.gerunn.parsing.tables.DefaultLog;
import ru.gerunn.parsing.tables.DefaultOperation;
import ru.gerunn.parsing.tables.DefaultType;

import jcifs.smb.SmbFile;

public class LogHandler<Type extends DefaultType, Operation extends DefaultOperation<Type>, Log extends DefaultLog<Operation>> {
	
	private LogProvider logProvider;
	private List<LogParser<Type, Operation, Log>> parsers;
	
	public LogHandler(LogProvider logProvider, List<LogParser<Type, Operation, Log>> parsers) {
		this.logProvider = logProvider;
		this.parsers = parsers;
	}

	public void handle() {
		for (SmbFile smbFile : logProvider.getFiles()) {
			System.out.println(smbFile.getPath());
			try {
				ZipInputStream zipInputStream = new ZipInputStream(smbFile.getInputStream());
				zipInputStream.getNextEntry();
				BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(zipInputStream, "UTF-8"));
				String line;
				while ((line = bufferedReader.readLine()) != null) {
					for (LogParser<Type, Operation, Log> parser : parsers) {
						parser.setString(line);
						parser.parse();
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
